package com.XinYun.Library.modules.borrow;

import com.XinYun.Library.modules.borrow.Service.Service;
import com.XinYun.Library.modules.borrow.Service.ServiceImpl;
import com.XinYun.Library.modules.commons.BasicServlet;
import com.XinYun.Library.utils.HTTPRequestUtils.HTTPRequestUtil;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "bookServlet", value = "/bookServlet")
public class bookServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = HTTPRequestUtil.getBody(request);
        //将JSON字符转化为JAVA对象
        System.out.println(request.getContextPath());
        JSONObject jsonObject = JSONObject.parseObject(json);
        String value = jsonObject.getString("uid");
        Service service = new ServiceImpl();
        Map<Object,Object> data1 = new HashMap<>();
        List<String> list;
        list=service.endtime(value);
        String s=null;
        int row = list.size();
        if(row == 0) {
            list.add("");
        }
        if(list.get(0) == "")
        {
            data1.put("code",-1);
            data1.put("message","获取信息失败");
        }
        else {
            s=list.get(0);
        }
        int time=service.late(value);
        int time1=service.late1(value);
        data1.put("时间",list);
        if(time==1){
            data1.put("code",1);
            data1.put("message","您的还书日期为"+s);
        }
        if(time1==1) {
            data1.put("code",2);
            data1.put("message", "已经逾期");
        }
        response.getWriter().write(data1.toString());
    }
}